using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using DataAccess.DbHelper;
using DataModel;
using Logic;
using SubSonic.Query;
using Common;
namespace SCM.Logic {
	/// <summary>
	/// D_Reconciliation表逻辑类
	/// </summary>
	public partial class D_ReconciliationBll : LogicBase {
 
 		/***********************************************************************
		 * 模版生成函数                                                        *
		 ***********************************************************************/
		#region 模版生成函数

		#region 单例模式
		//定义单例实体
		private static D_ReconciliationBll _D_ReconciliationBll = null;

		/// <summary>
		/// 获取本逻辑类单例
		/// </summary>
		/// <returns></returns>
		public static D_ReconciliationBll GetInstence() {
			if (_D_ReconciliationBll == null) {
				_D_ReconciliationBll = new D_ReconciliationBll();
			}
			return _D_ReconciliationBll;
		}
		#endregion

 		#region 获取D_Reconciliation表记录总数

		/// <summary>
		/// 获取D_Reconciliation表记录总数
		/// </summary>
		/// <returns>DataTable</returns>
		public int GetRecordCount(List<ConditionHelper.SqlqueryCondition> wheres) {
			var select = new SelectHelper();
			return select.GetRecordCount<D_Reconciliation>(wheres, false);

		}
		#endregion

		#region 获取D_Reconciliation表记录

		/// <summary>
		/// 获取一个DataTable
		/// </summary>
		/// <param name="norepeat">是否去重复</param>
		/// <param name="top">查询条数，0=全部记录</param>
		/// <param name="columns">要显示的列</param>
		/// <param name="pageIndex">当前页面，等于0时表示不分页，即查询全部记录</param>
		/// <param name="pageSize">页面大小，即当前页面显示多少条记录</param>
		/// <param name="wheres">条件</param>
		/// <param name="orders">排序</param>
		/// <returns>实体类</returns>
		//public DataTable GetDataTable(bool norepeat = false, int top = 0, List<string> columns = null, int pageIndex = 0, int pageSize = 0, List<ConditionHelper.SqlqueryCondition> wheres = null, List<string> orders = null) {
		public List<D_Reconciliation> GetDataTable(bool norepeat, int top , List<string> columns , int pageIndex, int pageSize, List<ConditionHelper.SqlqueryCondition> wheres, List<string> orders ) {
			try {
				//分页查询
				var select = new SelectHelper();
				return select.GetDataTable<D_Reconciliation>(norepeat, top, columns, pageIndex, pageSize, wheres, orders);
			}
			catch (Exception e) {
				//记录日志
				//CommonBll.WriteLog(page, "获取D_Reconciliation表记录时出现异常", e, true);

				throw e;
			}
		}
		#endregion
				
		#region 绑定Grid表格
		/// <summary>
		/// 绑定Grid表格，并实现分页
		/// </summary>
		/// <param name="page">当前页面指针</param>
		/// <param name="grid">表格控件</param>
		/// <param name="pageIndex">第几页</param>
		/// <param name="pageSize">每页显示记录数量</param>
		/// <param name="wheres">查询条件</param>
		/// <param name="orders">排序</param>
		//public void BindGrid(Page page, ExtAspNet.Grid grid, int pageIndex = 0, int pageSize = 0, List<ConditionHelper.SqlqueryCondition> wheres = null, List<string> orders = null) {
		public void BindGrid(ExtAspNet.Grid grid, int pageIndex, int pageSize, List<ConditionHelper.SqlqueryCondition> wheres , List<string> orders ) {
			//用于统计执行时长(耗时)
			//var swatch = new Stopwatch();
			//swatch.Start();

			try {
				// 1.设置总项数
				grid.RecordCount = GetRecordCount(wheres);

				if (grid.RecordCount == 0) {
					grid.Rows.Clear();
					return;
				}

				// 2.查询并绑定到Grid
				grid.DataSource = GetDataTable(false, 0, null, pageIndex, pageSize, wheres, orders);
				grid.DataBind();

			}
			catch (Exception e) {
				////记录日志
				//CommonBll.WriteLog(page, "获取用户操作日志表记录时出现异常", e, true);
				
				Common.LogHelper.Error(grid.Title +"获取时出现异常" , e);

			}

			//统计结束
			//swatch.Stop();
		
		}
		#endregion
		
		#region 添加D_Reconciliation表记录
		/// <summary>
		/// 添加D_Reconciliation记录
		/// </summary>
		/// <param name="model">D_Reconciliation表实体</param>
		public void Save(D_Reconciliation model) {
			try {

				model.Save();
				
				//更新后删除缓存
				//DelCache(page);
				
				//添加用户访问记录
				Common.LogHelper.Info(String.Format("{0} {1}",(model.IsNew() ? "添加" : "编辑") , "M_Supplier记录成功，ID=" + Convert.ToString(model.KeyValue())));

			}
			catch (Exception e) {
				String result = "执行D_ReconciliationLogic.Save()函数出错！";

				//出现异常，保存出错日志信息
				//CommonBll.WriteLog(page, result, e, true);
				Common.LogHelper.Error(result,e);

			}
		}
		#endregion

		#endregion 模版生成函数

    } 
}
